﻿Public Class Satker
    Inherits System.Web.UI.Page

    Dim koneksidataakses4 As New KoneksiDataAkses
    Dim kueri As String

    Sub bersih()
        textbox1.Text = ""
        textbox2.Text = ""
        textbox3.Text = ""
        textbox4.Text = ""
        GridView1.SelectedIndex = -1

        dropdownlist2.SelectedValue = 0
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        GridView1.DataBind()
        tabel1.Visible = False

    End Sub

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "btnCopy" Then
            bersih()
            textbox1.Text = GridView1.Rows(e.CommandArgument.ToString).Cells(1).Text
            textbox4.Text = GridView1.Rows(e.CommandArgument.ToString).Cells(2).Text
            textbox2.Text = GridView1.Rows(e.CommandArgument.ToString).Cells(3).Text
            textbox3.Text = GridView1.Rows(e.CommandArgument.ToString).Cells(4).Text

            If GridView1.Rows(e.CommandArgument.ToString).Cells(5).Text = "Aktif" Then
                dropdownlist2.SelectedValue = 0
            ElseIf GridView1.Rows(e.CommandArgument.ToString).Cells(5).Text = "Pasif" Then
                dropdownlist2.SelectedValue = 1
            End If
            tabel1.Visible = True
            System.Web.HttpContext.Current.Session("status_aksiy") = True
        End If
    End Sub

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles GridView1.SelectedIndexChanged

        textbox1.Text = GridView1.SelectedRow.Cells(1).Text
        textbox4.Text = GridView1.SelectedRow.Cells(2).Text
        textbox2.Text = GridView1.SelectedRow.Cells(3).Text
        textbox3.Text = GridView1.SelectedRow.Cells(4).Text

        If GridView1.SelectedRow.Cells(5).Text = "Aktif" Then
            dropdownlist2.SelectedValue = 0
        ElseIf GridView1.SelectedRow.Cells(5).Text = "Pasif" Then
            dropdownlist2.SelectedValue = 1
        End If
        System.Web.HttpContext.Current.Session("status_aksiy") = False
        tabel1.Visible = True
    End Sub

    Protected Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
        If Session("status_aksiy") = True Then
            koneksidataakses4.cekDuplikat("TB_MST_SATKER", "kode_satker", textbox1.Text)
            If koneksidataakses4.ds.Tables(0).Rows.Count = 0 Then
                kueri = "insert into TB_MST_SATKER(kode_satker,nama_satker,telp,lokasi_satker,is_active,tgl_update) values ('" + textbox1.Text + "', '" + textbox4.Text + "', '" + textbox2.Text + "','" + textbox3.Text + "', " + dropdownlist2.SelectedValue + ", CURRENT_TIMESTAMP )"
                koneksidataakses4.EksekusiNonQuery(kueri)
                GridView1.DataBind()
            Else
                Response.Write("ada data yang sama")
            End If
        ElseIf Session("status_aksiy") = False Then
            kueri = "update TB_MST_SATKER set kode_satker='" + textbox1.Text + "',nama_satker='" + textbox4.Text + "',telp='" + textbox2.Text + "',lokasi_satker='" + textbox3.Text + "',is_active=" + dropdownlist2.SelectedValue + ", tgl_update=current_timestamp where id_satker= " + GridView1.SelectedRow.Cells(0).Text + "  "
            koneksidataakses4.EksekusiNonQuery(kueri)
            GridView1.DataBind()
        End If
    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
        bersih()

        tabel1.Visible = True
        System.Web.HttpContext.Current.Session("status_aksiy") = True
    End Sub

    Protected Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button2.Click
        bersih()
    End Sub

    Protected Sub GridView1_PageIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles GridView1.PageIndexChanged
        bersih()
    End Sub

End Class